package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("travel")
public class TravelController {


    @Autowired
    private ITravelService travelService;
    // 待审核游记列表
    @RequestMapping("list")
    public String list(Model model , @ModelAttribute("qo") TravelQueryObject qo){
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_WAITING);
        qo.setOrderBy("t.createTime desc");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result",result);
        return "travel/list";
    }

    // 已发布游记列表
    @RequestMapping("releaseList")
    public String releaseList(Model model , @ModelAttribute("qo") TravelQueryObject qo){
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_RELEASE);
        qo.setOrderBy("t.releaseTime desc");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result",result);
        return "travel/releaseList";
    }
    // 拒绝发布游记列表
    @RequestMapping("refuseList")
    public String refuseList(Model model , @ModelAttribute("qo") TravelQueryObject qo){
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_FORBID);
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result",result);
        return "travel/refuseList";
    }
    // 改变游记状态
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel){
        JSONResult result = new JSONResult();
        try {
            travelService.changeState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @RequestMapping("getTravelById")
    @ResponseBody
    public TravelContent getTravelById(Long travelId){
        return  travelService.getTravelById(travelId);
    }
}
